package com.nowar.file.controller;

import com.alibaba.excel.EasyExcel;
import com.nowar.common.constant.CommonResultCode;
import com.nowar.common.result.response.ResponseCommonBase;
import com.nowar.common.util.DateUtil;
import com.nowar.common.util.LoggerFactoryUtil;
import com.nowar.file.entity.UserEntity;
import org.slf4j.Logger;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: 谢无殇。
 * @Date : 2023-01-15 22:35
 */
@RestController
@RequestMapping("/user")
public class FileController {
    private static Logger logger = LoggerFactoryUtil.getLoggerInfo();

    @GetMapping("/import")
    public ResponseCommonBase importExcel() throws IOException {
        logger.info("dsdsdsdsdsdsd");
        logger.error("1213644646");
        List<UserEntity> list = new ArrayList<>();
        UserEntity userEntity = UserEntity
                .builder()
                .uid("1001")
                .userName("小明")
                .address("深圳")
                .phone("12593")
                .createTime(DateUtil.format())
                .build();
        list.add(userEntity);
        ClassPathResource resource = new ClassPathResource("template/userTemplate.xlsx");
        EasyExcel.write("D:/clean/" + System.currentTimeMillis() + ".xlsx")
                .withTemplate(resource.getFile())
                .sheet()
                .doFill(list);
        /*
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        EasyExcel.write(outputStream).withTemplate("templateFileName").sheet().doFill(list);
        ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
        */
        return ResponseCommonBase.ok();
    }

}
